/*
 * @Author: xuetao 3168581151@qq.com
 * @Date: 2025-02-11
 * @LastEditors: xuetao 3168581151@qq.com
 * @LastEditTime: 2025-03-13
 * @Description: 
 */
import 'package:chat_client/enums/chat_enum.dart';
import 'package:chat_client/models/contact_model.dart';
import 'package:chat_client/models/function_item_model.dart';
import 'package:chat_client/providers/application_provider.dart';
import 'package:chat_client/widgets/custom_global_widgets.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:chat_client/router/router.dart';
import 'package:chat_client/utils/route_tools.dart' show generatePath;

class ContactScreen extends StatefulWidget {
  final Contact contact;
  final bool isBlack;
  const ContactScreen({super.key, required this.contact, this.isBlack = false});
  @override
  State<StatefulWidget> createState() => ContactScreenState();
}

class ContactScreenState extends State<ContactScreen> {
  final List<FunItem> _menuItems = [
    FunItem(
      title: '设置备注和标签',
      icon: Icons.edit_note,
      onTap: () {},
    ),
    FunItem(title: '朋友权限', icon: Icons.lock_outline),
    FunItem(title: '更多信息', icon: Icons.info_outline),
  ];

  @override
  Widget build(BuildContext context) {
    final applyModel = Provider.of<ApplicationProvider>(context);
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        actions: [
          !widget.isBlack
              ? IconButton(
                  onPressed: () {},
                  icon: Icon(Icons.more_horiz, size: 28),
                  padding: EdgeInsets.all(12),
                )
              : SizedBox.shrink(),
        ],
      ),
      body: Column(
        children: [
          // 个人信息卡片
          ProfileCard(
            name: widget.contact.nickname,
            account: widget.contact.username,
            location: widget.contact.region,
            avatarUrl: widget.contact.avatarUrl,
          ),
          // 功能列表
          Expanded(
            child: ListView.separated(
              physics: BouncingScrollPhysics(), // 回弹风格
              itemCount: !widget.isBlack ? _menuItems.length : 1,
              itemBuilder: (context, index) =>
                  _buildMenuItem(_menuItems[index]),
              separatorBuilder: (_, __) =>
                  Divider(height: 1, color: Colors.grey[300]),
            ),
          ),
          // 底部操作按钮
          widget.isBlack
              ? _buildUnblockButton(applyModel)
              : _buildActionButtons(),
        ],
      ),
    );
  }

  // 构建功能列表
  Widget _buildMenuItem(FunItem item) {
    return ListTile(
      tileColor: Colors.white,
      contentPadding: EdgeInsets.symmetric(horizontal: 24, vertical: 12),
      leading: Icon(item.icon, color: Colors.grey[700]),
      title: Text(item.title,
          style: TextStyle(fontSize: 17, fontWeight: FontWeight.w500)),
      trailing: Icon(Icons.chevron_right, color: Colors.grey[500]),
      onTap: item.onTap,
    );
  }

  // 构建底部操作按钮
  Widget _buildActionButtons() {
    return Container(
      color: Colors.white,
      child: Row(
        children: [
          Expanded(
            child: TextButton.icon(
              icon: Icon(
                Icons.videocam,
                size: 24,
                color: Color(0xFF5A6A91),
              ),
              label: Text('视频通话'),
              onPressed: () {},
            ),
          ),
          Container(width: 1, height: 40, color: Colors.grey[200]),
          Expanded(
            child: TextButton.icon(
              icon: Icon(Icons.send, size: 24, color: Color(0xFF5A6A91)),
              label: Text('发消息'),
              onPressed: () {
                RouteManager.navigateWithPayload(generatePath(
                    type: ConversationType.user.label,
                    id: widget.contact.userId));
              },
            ),
          ),
        ],
      ),
    );
  }

  //
  Widget _buildUnblockButton(ApplicationProvider applyModel) {
    return Container(
        color: Colors.white,
        width: double.infinity,
        child: TextButton.icon(
            icon: Icon(Icons.lock_open, size: 24, color: Color(0xFF5A6A91)),
            label: Text('解除拉黑'),
            onPressed: () async {
              await applyModel.handleBlackUser(widget.contact.userId, true);
              applyModel.navBack();
            }));
  }
}
